﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Jbe.TestSuite.Infrastructure.Interface.Documents;
using System.Windows.Documents;
using System.IO;
using System.Windows;
using Jbe.TestSuite.Repl.Properties;

namespace Jbe.TestSuite.Repl.Domain
{
    public class ReplDocumentType : DocumentType
    {
        public int documentCount;


        public ReplDocumentType()
            : base(Resources.CodeDocuments, "repf")
        {
        }

        public override bool CanNew() { return true; }

        protected override Document NewCore()
        {
            ReplDocument document = new ReplDocument(new FlowDocument());
            document.FileName = string.Format(Resources.DocumentTemplate + ".rb", ++documentCount);
            return document;
        }

        public override bool CanOpen() { return true; }

        protected override Document OpenCore(string fileName)
        {
            FlowDocument flowDocument = new FlowDocument();
            TextRange range = new TextRange(flowDocument.ContentStart, flowDocument.ContentEnd);
            using (FileStream stream = new FileStream(fileName, FileMode.Open))
            {
                range.Load(stream, DataFormats.Rtf);
            }

            ReplDocument document = new ReplDocument(flowDocument);
            documentCount++;
            return document;
        }

        public override bool CanSave(Document document) { return document is ReplDocument; }

        protected override void SaveCore(Document document, string fileName)
        {
            FlowDocument flowDocument = ((ReplDocument)document).Content;
            TextRange range = new TextRange(flowDocument.ContentStart, flowDocument.ContentEnd);
            using (FileStream stream = new FileStream(fileName, FileMode.Create))
            {
                range.Save(stream, DataFormats.Rtf);
            }
        }
    }
}
